﻿using Microsoft.Extensions.ObjectPool;
using System.Text;

namespace FanaticRock.Helpers
{
    internal static class StringHelper
    {
        ///<summary>字符串池。</summary>
        private static readonly ObjectPool<StringBuilder> _stringBuilderPool = new DefaultObjectPoolProvider().CreateStringBuilderPool();

        ///<summary>名称标准化，去除斜杠和空格</summary>
        public static string Normalize(string str)
        {
            var sb = _stringBuilderPool.Get();
            foreach (char c in str)
            {
                if (IsNormalChar(c))
                    sb.Append(c);
            }
            var result = sb.ToString();
            _stringBuilderPool.Return(sb);
            return result;
        }

        private static bool IsNormalChar(char c)
        {
            if (c == '/') return false;
            if (c == '\\') return false;
            if (c == ' ') return false;
            if (c == '\t') return false;
            if (c == '\r') return false;
            if (c == '\n') return false;
            if (c == ',') return false;
            if (c == '.') return false;
            return true;
        }
    }
}
